package kr.hyosang.android.penfo.adapter;

import java.util.ArrayList;

import kr.hyosang.android.penfo.R;
import kr.hyosang.android.penfo.SharedObject;
import kr.hyosang.android.penfo.data.Article;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;


public class FreeboardListAdapter extends BaseAdapter implements ListAdapter {
	private ArrayList<Article> data;
	
	public void setData(ArrayList<Article> list) {
		if(data == null) {
			data = list;
		}else {
			data.addAll(list);
		}
	}
	
	public int getListHeight(Context context) {
	    Resources res = context.getResources();
	    float item_height = res.getDimension(R.dimen.board_list_item_height) + res.getDimension(R.dimen.board_list_divider_height);
	    float total_height = (item_height * (float)getCount()) + res.getDimension(R.dimen.board_list_margin_bottom);
	    
	    return (int)total_height;
	}
	
	@Override
	public int getCount() {
		if(data != null) {
			return data.size();
		}
		
		return 0; 
	}

	@Override
	public Object getItem(int position) {
		if(data != null) {
			return data.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh;
		
		if(convertView == null) {
			Context context = SharedObject.getInstance().mAppContext;
			LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.board_list_item, null);
			
			vh = new ViewHolder();
			vh.mSubject = (TextView)convertView.findViewById(R.id.listArticleSubject);
			vh.mWriter = (TextView)convertView.findViewById(R.id.listArticleWriter);
			vh.mCategory = (TextView)convertView.findViewById(R.id.listArticleCategory);
			vh.mRegdate = (TextView)convertView.findViewById(R.id.listArticleRegdate);
			
			convertView.setTag(vh);
		}else {
			vh = (ViewHolder)convertView.getTag();
		}
		
		Article article = (Article)getItem(position);
		
		vh.mSubject.setText(article.subject);
		vh.mRegdate.setText(article.regdate);
		vh.mWriter.setText(article.writer);
		vh.mCategory.setText(article.category);

		return convertView;
	}
	
	class ViewHolder {
		public TextView mSubject;
		public TextView mWriter;
		public TextView mCategory;
		public TextView mRegdate;
	}



}
